package com.wicture.erp4android.ui;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.wicture.erp4android.R;
import com.wicture.erp4android.bean.Operator;
import com.wicture.erp4android.net.ServerUrl;
import com.wicture.erp4android.receiver.PDAReceiver;
import com.wicture.erp4android.utils.LogUtil;

/**
 * 登录页面
 * @author Administrator
 *
 */
public class LoginAct extends Activity implements OnClickListener{
	private EditText et_username;
	private EditText et_password;
	private Button btn_login;
	private Button btn_equit;
	private SharedPreferences sp;
	private String userName;
	private String password;
	
private Intent intent;
private PDAReceiver receiver;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_login);
		et_username = (EditText) findViewById(R.id.et_username);
		et_password = (EditText) findViewById(R.id.et_password);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_equit = (Button) findViewById(R.id.btn_equit);
		System.out.println("登录"+btn_login);
		btn_login.setOnClickListener(this);
		btn_equit.setOnClickListener(this);
		sp = getSharedPreferences("config", MODE_PRIVATE);
		receiver = new PDAReceiver() {
			
			@Override
			public void dispathCode(String code) {
				et_username.setText(code);
				
			}
		};
		IntentFilter scanDataIntentFilter = new IntentFilter();
		//scanDataIntentFilter.addAction("com.android.scancontext"); //前台输出
	scanDataIntentFilter.addAction("com.android.scanservice.scancontext"); //后台输出
		registerReceiver(receiver, scanDataIntentFilter);
	}
	
	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:
			userName = et_username.getText().toString();
			password = et_password.getText().toString();
			
			login(userName,password);
			
			
			break;
		case R.id.btn_equit:
			finish();
			break;
		default:
			break;
		}
	}
	private void login(final String username, final String password){
		HttpUtils http = new HttpUtils(5000);
		http.send(HttpMethod.GET, ServerUrl.loginUrl(username, password), new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException e, String arg1) {
				showMyToast("请检查网络！","服务器无响应");
				e.printStackTrace();
			}
			
			@Override
			public void onSuccess(ResponseInfo<String> resultInfo) {
				
				//保存账号密码
				LogUtil.i("登录结果", resultInfo.result);
				processData(resultInfo.result);
			
			}

			@Override
			public void onLoading(long total, long current, boolean isUploading) {
				// TODO Auto-generated method stub
				super.onLoading(total, current, isUploading);
			}
			
		});
//		toMainAct();
		
	}
	
	/**
	 * 解析响应数据
	 * @param data
	 */
	private void processData(String data){
		Gson gson = new Gson();
		Operator operator = gson.fromJson(data, Operator.class);
		if("success".equals(operator.RESULT)){
			Editor edit = sp.edit();
			edit.putString("user", userName);
			edit.putString("password", password);
			edit.putInt("userID", operator.DATA.USERID);
			edit.putString("userName", operator.DATA.USERNAME);
			edit.commit();
			Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
			toMainAct();
		}
	}
/**
 * 跳转到主界面
 */
	private void toMainAct(){
		if(intent==null){
		 intent = new Intent(this,MainAct.class);
		startActivity(intent);
		finish();
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(receiver); 
	}
	
	private void showMyToast(String title,String content){
		LayoutInflater inflater = getLayoutInflater();
		   View layout = inflater.inflate(R.layout.mytoast,
		     (ViewGroup) findViewById(R.id.llToast));
//		   ImageView image = (ImageView) layout
//		     .findViewById(R.id.tvImageToast);
//		   image.setImageResource(R.drawable.warn);
		   TextView tv_title = (TextView) layout.findViewById(R.id.tvTitleToast);
		   tv_title.setText(title);
		   TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
		   text.setText(content);
		   Toast toast = new Toast(getApplicationContext());
		   toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL, 12, 40);
		   toast.setDuration(Toast.LENGTH_SHORT);
		   toast.setView(layout);
		   toast.show();
	}
}
